package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.IMsgBaseCodeService;
import com.ibeeking.found.common.entity.MsgBaseCodeDTO;
import com.ibeeking.found.common.enums.msg.MsgCategoryEnum;
import com.ibeeking.found.common.enums.msg.MsgReceiverTypeEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.gmc.global.api.common.query.MsgBaseCodeFeignQuery;
import com.ibeeking.gmc.global.service.common.bo.MsgBaseCodeBO;
import com.ibeeking.gmc.global.service.common.constants.RedisKeyConstant;
import com.ibeeking.gmc.global.service.common.dos.MsgBaseCodeDO;
import com.ibeeking.gmc.global.service.common.query.MsgBaseCodeQuery;
import com.ibeeking.gmc.global.service.mapper.MsgBaseCodeMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.redis.annotation.CacheQuery;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MsgBaseCodeServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-01-05 14:41
 **/
@Service
public class MsgBaseCodeServiceImpl extends ServiceImpl<MsgBaseCodeMapper, MsgBaseCodeDO> implements IMsgBaseCodeService {

    @Resource
    private MsgBaseCodeMapper msgBaseCodeMapper;

    @Override
    public List<MsgBaseCodeBO> list(MsgBaseCodeQuery msgBaseCodeQuery) {
        List<MsgBaseCodeDO> msgBaseCodeDOS = msgBaseCodeMapper.selectList(new LambdaQueryWrapper<MsgBaseCodeDO>()
                .eq(msgBaseCodeQuery.getId() != null, MsgBaseCodeDO::getId, msgBaseCodeQuery.getId())
                .eq(StringUtils.isNotBlank(msgBaseCodeQuery.getCode()), MsgBaseCodeDO::getCode, msgBaseCodeQuery.getCode())
                .in(Collections3.isNotEmpty(msgBaseCodeQuery.getCodes()), MsgBaseCodeDO::getCode, msgBaseCodeQuery.getCodes())
                .like(StringUtils.isNotBlank(msgBaseCodeQuery.getName()), MsgBaseCodeDO::getName, msgBaseCodeQuery.getName())
                .eq(msgBaseCodeQuery.getReceiverType() != null, MsgBaseCodeDO::getReceiverType, msgBaseCodeQuery.getReceiverType())
                .eq(msgBaseCodeQuery.getCategory() != null, MsgBaseCodeDO::getCategory, msgBaseCodeQuery.getCategory())
                .orderByAsc(MsgBaseCodeDO::getReceiverType, MsgBaseCodeDO::getCategory, MsgBaseCodeDO::getSort)
        );
        List<MsgBaseCodeBO> msgBaseCodeBOS = JsonUtils.jsonToList(msgBaseCodeDOS, MsgBaseCodeBO.class);
        msgBaseCodeBOS.forEach(x -> wrapperMsgBaseInfoBO(x));
        return msgBaseCodeBOS;
    }


    @Override
    public MsgBaseCodeBO selById(Long id) {
        MsgBaseCodeDO msgBaseCodeDO = msgBaseCodeMapper.selectById(id);
        if (msgBaseCodeDO == null) {
            return null;
        }
        MsgBaseCodeBO msgBaseCodeBO = BeanUtil.convertBean(msgBaseCodeDO, MsgBaseCodeBO.class);
        wrapperMsgBaseInfoBO(msgBaseCodeBO);
        return msgBaseCodeBO;
    }

    @CacheQuery(cacheName = RedisKeyConstant.MODULE + RedisKeyConstant.GLOBAL_MSG_BASE_CODE, key = "#query.receiverType", expires = RedisKeyConstant.MONTH)
    @Override
    public List<MsgBaseCodeDTO> dtoList(MsgBaseCodeFeignQuery query) {
        MsgBaseCodeQuery msgBaseCodeQuery = BeanUtil.convertBean(query, MsgBaseCodeQuery.class);
        List<MsgBaseCodeBO> list = getBean().list(msgBaseCodeQuery);
        return JsonUtils.jsonToList(list, MsgBaseCodeDTO.class);
    }

    @Override
    public MsgBaseCodeDTO selDtoById(Long id) {
        MsgBaseCodeBO msgBaseCodeBO = getBean().selById(id);
        return BeanUtil.convertBean(msgBaseCodeBO, MsgBaseCodeDTO.class);
    }

    @Override
    public MsgBaseCodeDTO selDtoByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        List<MsgBaseCodeDO> msgBaseCodeDOS = msgBaseCodeMapper.selectList(Wrappers.lambdaQuery(MsgBaseCodeDO.class).eq(MsgBaseCodeDO::getCode, code));
        if (Collections3.isEmpty(msgBaseCodeDOS)) {
            return null;
        }
        DataUtils.trueThrowException(Collections3.size(msgBaseCodeDOS) > 1, "根据消息代码查询到多条消息配置信息");
        return BeanUtil.convertBean(msgBaseCodeDOS.get(0), MsgBaseCodeDTO.class);
    }


    public void wrapperMsgBaseInfoBO(MsgBaseCodeBO bo) {
        if (bo == null) {
            return;
        }
        bo.setCategoryName(MsgCategoryEnum.getNameByCode(bo.getCategory()));
        bo.setReceiverTypeName(MsgReceiverTypeEnum.getNameByCode(bo.getReceiverType()));
    }


    private IMsgBaseCodeService getBean() {
        return SpringBeanUtil.getBean(IMsgBaseCodeService.class);
    }

}
